package com.example.second_home_work


import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class MainActivity : AppCompatActivity() {
    // 定义模型
    private lateinit var model: ProgramModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        // 初始化模型
        model = ProgramModel(this)

        // 区1：动态添加TextView
        val linearLayout = findViewById<LinearLayout>(R.id.linearLayout)
        val addTextViewButton = findViewById<Button>(R.id.addTextViewButton)

        addTextViewButton.setOnClickListener {
            val newTextView = TextView(this)
            newTextView.text = getString(R.string.dynamic_text)
            newTextView.layoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            )
            linearLayout.addView(newTextView) // 动态添加TextView到LinearLayout中
        }

        // 区2：MVC Program Adviser 交互
        val queryButton = findViewById<Button>(R.id.queryButton)
        val inputField = findViewById<EditText>(R.id.inputField)
        val resultTextView = findViewById<TextView>(R.id.resultTextView)

        queryButton.setOnClickListener {
            val userInput = inputField.text.toString().trim()
            val advice = model.getProgramAdvice(userInput)
            resultTextView.text = advice
        }
    }


}
